package com.bookmall.cart.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.bookmall.cart.domain.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000119678152";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDTrQwpV/0XYOzZwviPGxDwdnXBHW4jqCu4ce6JgGBWhXYAO+1C12bIwmiekpCccj9LpzmR9RXEYZ10Caer5JXC7REr6sQRJq536zYlEkSleq4GpNLRee7BJvQnsl1B1YVHqmNjxXLY4ynSBccXY8Kef5dR9V4N+KHANyZoMYCzbh5l/MeK9bZ2WDblr4NZCpqxCdGE96bqBGKr+deQPyVxAUQtVxIJyxrACYbYls08rSVziH8i3r3KyMCUbpn3hc5qlXuSBPKPYKL+ZS7WjlYc3wT9V8o0g9frgiDNDFj+TXkly50eucl1sBOCcO80LvjVFmp7DGAAphCYr1++hypHAgMBAAECggEBAJnEg6kk+f4ZsvUd5WJPzyZfZ3s53eCZMelzGk1YrKMtoGLbJagAwfHlCArpfZOoisJyYCffWkimgLLisKL2ErTfcjHtATFUYDlM7LBRbPyo+fJpmZwCb6ZwnbyYnZZNfdxpsnmv3ZYgryiaed5L4f0xwsMO85xCmX4Aki3uiAfPDKBty0KC9fTbtBN9qlOHZwXKVsPvVsr3/IdSrTkoRee5MHTI0r29HM+VNLW9dp6IFeAP2utkkRMC5oNaXm/zsaajT6yzxrq6v/t95vA6vsMSPIUFF2TBfwIXPF0AJDsony222+gpLnl4zGUxcV0ANQKd1MUXDxCHteHuzyTZvsECgYEA6QgQj2L6APhrGi4TbMjfRNR7yeZLGLT1vFMNLC9/fij3tjdXO2vhSrEdVxHi+ZS6Div6ZVLk6250QioRzbbMEPhbfZ9qo8QNDCR69CnVejdBExw7ERIzxXI/C4z/edGkCOQI3f3x9xyTwYE81noV3o1MI9IICA+YMjIE+gCxcKUCgYEA6Ioh18tVAwLiyGgEYB4TiGE3Bd2Ik+qOgH7I2qJSTokK2BeA5RQgpZ4IVx8twmJlfryph7+3w92p61itZ2f9UAn3n9MCTotOzLgdPEbCrCiiNLC1U8YT06XslQipc1qJNJmSK6rtTDQpfswWggsXrBigjUewkK8+RcOwMvXs73sCgYEA2aDLfm2sF73ooFGJmJ3cTba3SO5HEfLmUBOsCKjvQ05VURpL4FW22H+ibA9rTzUDsXqG5ZvN6MWF08jgSaM+31Zic2avO/Q7Tyy+m0p6cSIrJwLFcwLx0KMKl+n9KbKMpDT6xv+Duwk4QI5G1/K6e9av1gAvuf9+Xtpx5V1CWXUCgYBStJZW1uW0fXlFR+GOggP2dIUKXAFwgnMOdolfYW4eqpUlyC46y47U+MEEo3017qMkkVtGgcSNMDB4GQELjyvEJNdii7z9TITJQQ582rnwWz9duB+13PQ+SXw8gWkrMt8ryxb22wMtIDxF8bZrqCBSQCwSM5Yzz3A5heEMLvWHMwKBgQCY5/e/BXwK5JfbFIrcOik4opHP2M0mQu+FR12egXcO+1GtKEtcOUM2HlWb6qzIZLQh+JCraXB0A4YoJs7nAW741zgueECImx0b2SDMHM9+mpUfsDQeCsLrukiM9cEfQY4LjsiK12AGk8MHTrhulLMrEY98JNqTbPPBhybV9dV7hQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjj114rseWXo15OnvUxWuuqubkzD3FGGtcGj54WLZt0znZ60E7FiUlSa81HYSgyA82BTa4FoB2EcSFpZ71ECYCI1jAcb2qp2yTO8lzsdg0+13hP/XZrneXrqhbL/aAozYPSnJmwKZJErE1wOzlHFbWFVFsjVCxg12mfU9AZlrJ2s8rv4SbO5jqFZISy6l5hWxDktDof0Xc8eUO685PAdi7ugo2an0vHYVcj/FJu1WPArIEBB/Fq27zX1+yH8n1kMWQ9Vf5/kDEpY8QFgWLnuQk4f0J5HDEOIjb8ZyFoVdWXLkiGt6h9jK45sg35tBe3WjNtGkq+vHSsyscrlZ47UK0QIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://huyulu666.top/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://huyulu666.top/center.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "5m";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;
    }
}
